<template>
	<view>
		<view class="my-search">
			<MySearch @gotosearch="gotosearch"></MySearch>
		</view>
		<swiper indicator-dots autoplay interval="3000" circular duration="1000">
			<swiper-item v-for="(item,index) in swiperList" :key="index">
				<navigator class="swiper-item" :url="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`">
					<image :src="item.image_src"></image>
				</navigator>
			</swiper-item>
		</swiper>
		<view class="nav-list">
			<view class="nav-item" v-for="(item,index) in navList" :key="index" @click="gotocate(item)">
				<image class="nav-img" :src="item.image_src"></image>
			</view>
		</view>
		<view class="floor-list">
			<!-- 楼层 item 项 -->
			<view class="floor-item" v-for="(item, index) in floorList" :key="index">
				<!-- 楼层标题 -->
				<image :src="item.floor_title.image_src" class="floor-title"></image>
				<!-- 楼层图片区域 -->
				<view class="floor-img-box">
					<!-- 左侧大图片的盒子 -->
					<view class="left-img-box" @click="gotogoodslist(item.product_list[0])">
						<image :src="item.product_list[0].image_src" :style="{width:
				item.product_list[0].image_width + 'rpx'}" mode="widthFix"></image>
					</view>
					<!-- 右侧 4 个小图片的盒子 -->
					<view class="right-img-box">
						<view class="right-img-item" v-for="(item2, i2) in item.product_list" :key="i2"
							@click="gotogoodslist(item2)">
							<image :src="item2.image_src" mode="widthFix" :style="{width:
				item2.image_width + 'rpx'}" v-if="i2 !== 0"></image>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		reactive
	} from 'vue';
	import {
		onLoad
	} from '@dcloudio/uni-app';
	import { useSetTabBarBadge } from '@/hooks/useSetTabBarBadge.js';
	// 使用组合式函数
	const { setBadge } = useSetTabBarBadge();
	//轮播图数据
	const swiperList = reactive([]);
	//分类导航的数据列表
	const navList = ref([])
	// 楼层的数据列表
	const floorList = reactive([])
	//小程序页面生命周期
	onLoad(() => {
		getSwiperList()
		getnavList()
		getfloorList()
	})
	//轮播图数据请求
	const getSwiperList = async () => {
		const {
			data: res
		} = await uni.$http.get('/api/public/v1/home/swiperdata')
		// console.log(res);
		if (res.meta.status !== 200) {
			return uni.$showMsg()
		}
		swiperList.length = 0; // 清空原数组
		swiperList.push(...res.message)
		// console.log(swiperList);
		// uni.$showMsg('数据请求成功')
	}
	//请求分类导航的数据列表
	const getnavList = async () => {
		const {
			data: res
		} = await uni.$http.get('/api/public/v1/home/catitems')
		if (res.meta.status == 200) {
			// console.log(res);
			navList.value.length = 0
			navList.value.push(...res.message)
		} else {
			return uni.$showMsg()
		}
	}
	//请求楼层的数据列表
	const getfloorList = async () => {
		const {
			data: res
		} = await uni.$http.get('/api/public/v1/home/floordata')
		// console.log(res);
		if (res.meta.status !== 200) {
			return uni.$showMsg()
		}
		// 通过双层 forEach 循环，处理 URL 地址
		res.message.forEach(floor => {
			floor.product_list.forEach(prod => {
				prod.url = '/subpkg/goods_list/goods_list?' +
					prod.navigator_url.split('?')[1]
			})
		})
		floorList.length = 0; // 清空原数组
		floorList.push(...res.message)
		// console.log(floorList);
	}
	//跳转到分类页面
	const gotocate = (item) => {
		// 判断点击的是哪个 nav
		if (item.name === '分类') {
			uni.switchTab({
				url: '/pages/cate/cate'
			})
		}
	}
	//跳转商品列表页面
	const gotogoodslist = (item) => {
		uni.navigateTo({
			url: item.url
		})
	}
	//跳转搜索页面
	const gotosearch=()=>{
		uni.navigateTo({
			url:'/subpkg/search/search'
		})
	}
</script>

<style lang="scss">
	.my-search {
		position: sticky;
		top:0;
		z-index: 999;
	}
	swiper {
		height: 330rpx;

		.swiper-item,
		image {
			width: 100%;
			height: 100%;
		}
	}

	.nav-list {
		display: flex;
		justify-content: space-around;
		margin: 15px 0;

		.nav-img {
			width: 128rpx;
			height: 140rpx;
		}
	}

	.floor-title {
		height: 60rpx;
		width: 100%;
		display: flex;
	}

	.right-img-box {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-around;
	}

	.floor-img-box {
		display: flex;
		padding-left: 10rpx;
	}
</style>